from openstack import connection

# create connection
username = "cangoitadmin"
password = "op17afv71$n"
projectId = "d19c236bc28a403788989e19b4d2e055"    # tenant ID
userDomainId = "875611680d1c43d1b1966f656e1c3342"    # user account ID
auth_url = "https://iam.cn-east-2.myhuaweicloud.com/v3"    # endpoint url
zone_name = "cango.local"
zone_id=""

conn = connection.Connection(auth_url=auth_url,
                             user_domain_id=userDomainId,
                             project_id=projectId,
                             username=username,
                             password=password)

# # set parameters
# limit = 5
# # define function for listing servers
# def list_servers():
#     # get server list with params
#     servers = conn.compute.servers(limit=limit)
#     # iterate servers list
#     for server in servers:
#         print(server)

def create_recordset(conn):
    recordset = {
        "name": "zhengzu.cangoonline.net.",
        "description": "This is an example record set.",
        "type": "A",
        "ttl": 3600,
        "update": "8aace3ba63f4bb80016541cae0da2d3c",
        "records": [
            "1.2.3.4"
        ]
    }

    recordset = conn.dns.create_recordset('8aace3ba63f4bb8001653cf1979e0f47',
                                          **recordset)
    print recordset
    return recordset

create_recordset(conn)


#
# def list_recordsets(conn,zone_name):
#
#     zoneid = HuaweiCloud.objects.filter(zone_name=zone_name).zone_id
#
#     recordsets = conn.dns.recordsets(zoneid)
#     for recordset in recordsets:
#         if recordset.type == "A":
#            #print recordset
#            print(str.strip(str(recordset.name),str(recordset.zone_name)) + str(recordset.records))
#
#     #     def get_recordset(conn, recordset_id):
#     #         zone = Zone(id='ff8080825ca865e8015caa9f452700a8')
#     #         recordset = conn.dns.get_recordset(zone, recordset_id)
#     #         recordset = conn.dns.get_recordset(zone, Recordset(id=recordset_id))
#     #         recordset = conn.dns.get_recordset(zone.id, recordset_id)
#     #         recordset = conn.dns.get_recordset(zone.id, Recordset(id=recordset_id))
#     #         logging.info(recordset)

# visit API
# list_servers()

